package ui;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class FileDisplay {

	private String filePath;
	
	public FileDisplay(String filepath) {
		//create the window
		//read the file
		//
		this.filePath = filepath;
		initWindow();
	}
	
	private void initWindow() {
		JFrame mainFrame = new JFrame(filePath);
		mainFrame.setSize(500, 400);
		mainFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		if(addFile(mainFrame)) {
			mainFrame.setVisible(true);
		}
	}
	
	private boolean addFile(JFrame mainFrame) {
		
		JTextArea displayArea = new JTextArea();
		JScrollPane scrollPane;
		String fileText = "";
		String line = null;

		try {
			BufferedReader fileReader = new BufferedReader(new FileReader(filePath));
			while ((line = fileReader.readLine()) != null){
				fileText = fileText.concat(line + '\n');
			}
			fileReader.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(mainFrame,
				    "Error displaying file.",
				    "Display Error",
				    JOptionPane.WARNING_MESSAGE);
			return false;
		}

		displayArea.setEditable(false);
		displayArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));		
		displayArea.setText(fileText);
		displayArea.setCaretPosition(0);
		
		scrollPane = new JScrollPane(displayArea);
		mainFrame.getContentPane().add(scrollPane);
		
		return true;
	} //addFile
	
}//end FileDisplay
